<?php

function form_validation($form) {

//print_r($form); die;
  $stat = new set_static_variable();

//check the corresponding concerned dept name
//chk_concerned_dept($form['field_form_dept']);

  //Duplication in title
  //check_title_duplication($form['title'], $form['type']['#value'], $form['field_form_dept'], 'field_form_dept', $stat->get_nid());
  
  $stat->set_nid(0);
  
  // District
  distvalidation($form['field_form_dist']['und']['#value']);
  
	
 	// minimum length charcter
  	minilength_validation($form['title']['#value'], 5, $form['title']['#title'], 'title');
  	minilength_validation($form['field_form_keywords']['und'][0]['value']['#value'], 5, $form['field_form_keywords']['und'][0]['value']['#title'],'field_form_keywords');
  	minilength_validation($form['field_form_desc']['und'][0]['value']['#value'], 5, $form['field_form_desc']['und'][0]['value']['#title'], 'field_form_desc');
	
 	//maximum length validation for Title and Description Field
  	maxlength_validation($form['title']['#value'], 255, $form['title']['#title'], 'title');
	

	
  // Bad character validations  

  chkbadchar($form['title']['#value'], $form['title']['#title'], 'title'); 
  chkbadchar($form['field_form_ifany'][0]['value']['#value'], $form['field_form_ifany']['#title'], 'field_form_ifany');
  chkbadchar($form['field_form_keywords']['und'][0]['value']['#value'], $form['field_form_keywords']['und'][0]['value']['#title'], 'field_form_keywords');
  chkbadchar($form['field_form_desc']['und'][0]['value']['#value'], $form['field_form_desc']['und'][0]['value']['#title'], 'field_form_desc'); 
  chkbadchar($form['field_form_ifany'][0]['value']['#value'], $form['field_form_ifany']['#title'], 'field_form_ifany'); 
  chkbadchar($form['field_form_othercategory'][0]['value']['#value'], $form['field_form_othercategory']['#title'], 'field_form_othercategory');
  chkbadchar($form['field_form_othertype'][0]['value']['#value'], $form['field_form_othertype']['#title'], 'field_form_othertype');
  // Date Validation  

  check_scheduler_date_expire($form['scheduler_settings']['unpublish_on']['date']['#value'],'unpublish_on');
  
 	// District
	if(sizeof($form['field_form_dis']['value']['#value'])>1 && $form['field_form_dis']['value']['#value'][0]==0){
    
        $field=$form['field_form_dis']['value']['#value'][0];
        $field_title=$form['field_form_dis']['#title'];
         
     
    form_set_error($field, $field_title . ' field can have either \'ALL\' or \'Other Districts\'');
    
    }
 
  // Form Type validtion

  $form_type = $form['field_form_type'];
  if ($form_type['value']['#value'] == 'online') {
  	
		$download = $form_type['value']['#value'] == 'download';
		$down =  $form['field_form_upload'][0]['#value']['filename']; 
		if(!empty($down) ) {
		
		form_set_error('field_form_upload', t('Please remove Download pdf', array(
														   '%name' => $form['field_form_upload']['#title'])));
		}
		
    $reference_url = $form['field_form_avail'][0]['value']['#value'];
    $reference_url = url_validation($reference_url);
 
    if(isURL($reference_url) == 0 && $reference_url != '') {
      form_set_error('field_form_avail', t('Not a valid URL given in %name field', array(
                                                   '%name' => $form['field_form_avail']['#title'])));    
    }
    elseif ($reference_url == '') {
      form_set_error('field_form_avail', t(' %name field is required', array(
                                                   '%name' => $form['field_form_avail']['#title'])));
    }
  }
  elseif ($form_type['value']['#value'] == 'download') {
  	$online = $form_type['value']['#value'] == 'online';
	$online = '';
    // Validating the file size   
    $field_obj = $form['field_form_upload']; 
	$file_allowed_obj = $form['field_form_upload'];
    file_upload_validation($field_obj, $file_allowed_obj, 'field_form_upload');  
  }  

} //form_validaiton()


  function form_presave_validation($node) {
    $reference_url = $node->field_form_avail[0]['value'];
    $node->field_form_avail[0]['value'] = url_validation($reference_url);   
  } // function form_presave_validation()
   




